package com.lzh.prometheus.message;

import com.lzh.prometheus.httpclient.DingdingHttpClient;
import com.lzh.prometheus.pojos.PromethuesNotice;
import com.lzh.prometheus.pojos.dingding.DingDingNotice;
import com.lzh.prometheus.pojos.dingding.DingDingResult;
import com.lzh.prometheus.properties.notice.DingDingNoticeProperty;
import com.lzh.prometheus.text.NoticeTextResolver;
import com.taobao.api.ApiException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author zhehen.lu
 * @date 2025/8/15 9:23
 */
public class DingDingNoticeSendComponent <T extends PromethuesNotice> implements INoticeSendComponent<T> {

    private final DingdingHttpClient httpClient;

    private final NoticeTextResolver<T> noticeResolver;

    private final DingDingNoticeProperty dingDingNoticeProperty;

    private final Log logger = LogFactory.getLog(DingDingNoticeSendComponent.class);

    /**
     * @param httpClient
     * @param exceptionNoticeResolver
     * @param dingDingNoticeProperty
     */
    public DingDingNoticeSendComponent(DingdingHttpClient httpClient, NoticeTextResolver<T> noticeResolver,
                                       DingDingNoticeProperty dingDingNoticeProperty) {
        this.httpClient = httpClient;
        this.noticeResolver = noticeResolver;
        this.dingDingNoticeProperty = dingDingNoticeProperty;
    }

    /**
     * @return the httpClient
     */
    public DingdingHttpClient getHttpClient() {
        return httpClient;
    }

    @Override
    public void send(T exceptionNotice) throws ApiException {
        String noticeText = noticeResolver.resolve(exceptionNotice);
        DingDingNotice dingDingNotice = dingDingNoticeProperty.generateDingdingNotice(noticeText,
                exceptionNotice.getTitle());
        DingDingResult result = httpClient.doSend(dingDingNotice);
        logger.debug(result);
    }

}